﻿var AdjBom = {};
//设置
AdjBom.settings = {
    TaskId: com.get.TaskId,
    TaskCode: "",
    editRow: undefined,
    ColumnsParam: [],
    currentTr: {},
    EquipId: 0
};

AdjBom.init = function () {

    $("#TaskId").val(AdjBom.settings.TaskId);
    var Xuhao = com.get.Xuhao;
    var OrgId = com.get.OrgId;
    //加载任务信息
    com.ajax({
        url: "/Logic/TM/TaskManagerAjax.aspx?method=GetTaskInfoByPrimaryId&TaskId=" + AdjBom.settings.TaskId, success: function (data) {
            $("#lblXuhao").html(Xuhao);
            $("#lblTaskCode").html(data.TaskCode);
            $("#lblProjName").html(data.ProjName);
            $("#lblEngName").html(data.EngName);
            $("#lblEquipName").html(data.EquipName);
            $("#lblEquipModel").html(data.EquipModel);
            $("#lblNum").html(data.TaskNum);
            $("#hidEquipId").val(data.EquipId);
            AdjBom.settings.TaskCode = data.TaskCode;
            AdjBom.settings.EquipId = data.EquipId;
            AdjBom.settings.EquipModel = data.EquipModel;
        }
    });

        var height = $("#divTotal").height();
        var master = $("#master").height();
        var content = parseFloat(height) - parseFloat(master) - 50;
        $("#tbList").css("height", content);




        //加载原始数据列表
        $("#tbList").treegrid({
            url: '/Logic/TM/OrgDataAjax.aspx?method=GetTreeOrgDataByTaskId',
            loadMsg: '数据加载中请稍后……',
            // fitColumns: true,
            queryParams: { TaskId: AdjBom.settings.TaskId,OrgId:OrgId },
            idField: "ID",
            treeField: "ID",
            singleSelect: false,
            autoRowHeight: true,
            nowrap: false,
            striped: true,
            frozenColumns: [[{ field: 'ck', checkbox: true, width: 30 },


             {
                 field: 'ID', title: '序号', align: "left", halign: "center", width: 160, formatter: function (value, row, Index) {

                     return row.Xuhao;
                 }
             }, { field: 'Zongxu', title: '总序', align: "center", width: 60 },
             { field: 'MsXuhao', title: '明细序号', align: "center", width: 60 },
               { field: 'PartName', title: '子项名称', align: "center", width: 140 },
             { field: 'MaterialName', title: '物料名称', align: "center", width: 140 },
             { field: 'Map', title: '图号', align: "center", width: 120 },
             { field: 'MaterialCode', title: '物料编码', align: "center", width: 100 }
            ]],
            columns: [[
             { field: 'MaterialGuige', title: '规格', align: "center", width: 120 },
             { field: 'Ku', title: 'K ', align: "center", width: 20 },
           //  { field: 'KeyPart', title: '关键部件 ', align: "center" },
             { field: 'SingleNum', title: '单台数量 ', align: "center", width: 40 },
             { field: 'Num', title: '数量 ', align: "center", width: 40 },
             { field: 'UnitWeight', title: '单重(kg) ', align: "center", width: 70 },
             { field: 'TotalWeight', title: '总重(kg) ', align: "center", width: 70 },
             { field: 'MaterialGB', title: '国标', align: "center", width: 100 },
             { field: 'HidGuige', title: '材料规格', align: "center", width: 100 },
             { field: 'Length', title: '材料长度', align: "center", width: 70 },
             { field: 'Width', title: '材料宽度 ', align: "center", width: 70 },
             { field: 'MaterialTheroWeight', title: '理论重量', align: "center", width: 60 },
             { field: 'MaUnitWeight', title: '材料单重', align: "center", width: 70 },
             { field: 'MaTotalWeight', title: '材料总重', align: "center", width: 70 },
             { field: 'TotalLength', title: '材料总长', align: "center", width: 70 },
             { field: 'Area', title: '面域', align: "center", width: 70 },
             { field: 'PlanArea', title: '计划面域', align: "center", width: 70 },
             { field: 'MaterialCaizhi', title: '材质 ', align: "center", width: 100 },
             { field: 'MaState', title: '毛胚状态 ', align: "center", width: 70 },
               {
                   field: 'Process', title: '工艺流程 ', align: "center", width: 70, formatter: function (value, row, Index) {

                       return com.getArrayText(StaticCombo.Process, value);
                   }
               },
             { field: 'IsFixedSize', title: '是否定尺 ', align: "center", width: 60 },
             { field: 'SelfWeight', title: '部件自重 ', align: "center", width: 70 },
             { field: 'IsSubmitMs', title: '制作明细 ', align: "center", width: 60 },
             { field: 'IsSubmitMp', title: '材料计划 ', align: "center", width: 60 },
              { field: 'ClassName', title: '制作班组 ', align: "center", width: 60 },
             { field: 'Note', title: '备注', align: "center", width: 250 }
            ]]
        });


    // 初始化父类Combo
    $("#IsAdjustMapXuhao").combobox({
        data: StaticCombo.Pick,
        valueField: 'value',
        textField: 'text',
        panelHeight: 'auto',
        editable: false,
    });


    //初始化列显示隐藏界面
    $('#divAdjust').dialog({
        title: '调整序号',
        width: 230,
        height: 150,
        collapsible: true,
        minimizable: false,
        closable: true,
        cache: false,
        buttons: [{
            text: "确定", icon: "icon-save", handler: function () {
                var rows = $("#tbList").treegrid("getSelections");
                var Xuhao = $("#comboParent").val();
                if (rows.length > 0 && rows.length < 2) {
                    com.ajax({
                        url: "/Logic/TM/OrgDataAjax.aspx?method=ChangeOrgParent", data: { orgId: rows[0].ID, Xuhao: Xuhao, TaskId: AdjBom.settings.TaskId, IsAdjustMapXuhao: $("#IsAdjustMapXuhao").combobox("getValue") }, success: function (data) {
                            $("#tbList").treegrid("unselectAll");
                            $("#tbList").treegrid("load");

                            $("#divAdjust").dialog("close");
                            com.message(data.state, data.msg, function () {

                            });

                        }
                    });
                }
                else {
                    com.message("information", "请选择一条数据");
                }
            }
        }, { text: "取消", handler: function () { $("#divAdjust").dialog("close"); } }]
    });
    $("#divAdjust").dialog("close");


    //初始化拆分窗口
    $('#divDivide').dialog({
        title: '编辑窗口',
        width: 230,
        height: 150,
        collapsible: true,
        minimizable: false,
        closable: true,
        cache: false,
        buttons: [{
            text: "确定", icon: "icon-save", handler: function () {

                var rows = $("#tbList").treegrid("getSelections");
                var splitNum = $('#txtDivide').numberbox('getValue');
                if (splitNum == 0 || splitNum == "") {
                    com.message("information", "拆分数量不能为空！");
                    return;
                }
                if (rows.length > 0 && rows.length < 2) {
                    com.ajax({
                        url: "/Logic/TM/OrgDataAjax.aspx?method=SplitOrg", data: { orgId: rows[0].ID, splitNum: splitNum, TaskId: AdjBom.settings.TaskId, Xuhao: $("#txtNewXuhao").val() }, success: function (data) {

                            $("#tbList").treegrid("load");
                            com.message(data.state, data.msg, function () {

                            });

                        }
                    });

                } else {
                    com.message("information", "请选择一条数据");
                }
            }
        }, { text: "取消", handler: function () { $("#divDivide").dialog("close"); } }]
    });
    $("#divDivide").dialog("close");


}



//编辑
AdjBom.EditOrgData = function () {
    var rows = $("#tbList").treegrid("getSelections");
    var orgIds = "";
    if (rows.length > 0) {
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].MpReviewState == "2" || rows[i].MsReviewState == "2" || rows[i].OsReviewState == "2" || rows[i].MpReviewState == "1" || rows[i].MsReviewState == "1" || rows[i].OsReviewState == "1") {
                com.message("information", "该条记录正在审核中，无法编辑");
                return;
            }
            else {
                orgIds += rows[i].ID + ",";
            }
        }
        top.wrapper.addTab("原始数据编辑", "/UI/TM/OrgDetailEdit.aspx?action=edit&TaskId=" + AdjBom.settings.TaskId + "&orgIds=" + orgIds.trim(','));

    }
    else {
        com.message("information", "请选中一行");
    }
}

AdjBom.DeleteOrg = function () {
    var rows = $("#tbList").treegrid("getSelections");
    var Xuhao = "";
    if (rows.length > 0) {
        for (var i = 0; i < rows.length; i++) {
            Xuhao += rows[i].Xuhao + ","
        }
        Xuhao.trim(',');
        com.message("confirm", "您确定要删除明细序号为【" + Xuhao + "】的记录吗？", function (r) {
            if (r) {
                //  alert("a");
                com.ajax({
                    url: "/Logic/TM/OrgDataAjax.aspx?method=DeleteOrg", data: { TaskId: AdjBom.settings.TaskId, data: JSON.stringify(rows) }, success: function (data) {
                        com.message(data.state, data.msg, function () {

                            for (var i = 0; i < rows.length; i++) {
                                $("#tbList").treegrid("load");

                            }

                        })
                    }
                });
            }

        })
    }
    else {
        com.message("information", "请选中一行");
    }


}


AdjBom.AdjParent = function () {

    $("#divAdjust").dialog("open");

}

AdjBom.Divide = function () {

    $("#divDivide").dialog("open");

}

$(AdjBom.init);